/**
 * @FileName: VnetService.java
 * @Author
 * @Description:
 * @Date 2016年9月1日 上午10:52:37
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.ActivityTariffClassRelation;
import cn.hpclub.server.model.Organization;
import cn.hpclub.server.model.TariffClassRelation;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.util.ObjectUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.quartz.core.QuartzScheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityTariffClassRelationService {
    private static final Logger      logger  = LoggerFactory.getLogger(QuartzScheduler.class);
    public static ActivityTariffClassRelationService service = new ActivityTariffClassRelationService();


    public int deleteRelationByTariffClassId(int tariffClassId){
        String sql = "DELETE FROM activity_tariff_class_relation WHERE tariff_class_id = ?";
        int number = Db.update(sql,tariffClassId);
        return number;
    }
    public int deleteRelationByTariffClassIds(List<String> tariffClassIds){
        StringBuffer sql = new StringBuffer("DELETE FROM activity_tariff_class_relation WHERE tariff_class_id in(");
        for(int i=0;i<tariffClassIds.size();i++){
            sql.append(Integer.parseInt(tariffClassIds.get(i).toString()));
            if(i < tariffClassIds.size() - 1){
                sql.append(",");
            }
        }
        sql.append(" )");
        int number = Db.update(sql.toString());
        return number;
    }

    public boolean getTariffByCityAndCode(String type,int activityId){
        StringBuffer sql = new StringBuffer("SELECT * FROM activity WHERE type=? ");
        if(activityId > 0){
            sql.append(" AND id !=" + activityId);
        }
       /* List<Record> list = getCityOrgId(orgId);
        if(ObjectUtil.isNotEmpty(list) && list.size() > 0){
            int city_org_id = list.get(0).getInt("id");
            sql.append(" AND  org_id in ").append(InterfaceUtil.getChlidOrgIdSql(city_org_id, Constant.MAX_ORG_LEVEL, true));
        }*/
        List<Record> tariffList = Db.find(sql.toString(), type);
        return tariffList.size() > 0 ? true : false;
    }



    public void saveRelation(int tariffId,List<Integer> activityIdList){
        for (Integer activityId : activityIdList) {
            ActivityTariffClassRelation activityTariffClassRelation = new ActivityTariffClassRelation();
            activityTariffClassRelation.set(activityTariffClassRelation.ACTIVITY_ID,activityId);
            activityTariffClassRelation.set(activityTariffClassRelation.TARIFF_CLASS_ID,tariffId);
            activityTariffClassRelation.save();
        }
    }


    public String getSqlExceptSelect(ConcurrentHashMap<String, String> searchInfo){
        String sqlExceptSelect = " FROM activity a LEFT JOIN sys_code_value sc ON a.type = sc.`value`" +
                " LEFT JOIN sys_code_type st ON sc.type_id = st.id" +
                " WHERE st.`code` = 'HDLX' AND a.is_deleted=0  ";

        for(Map.Entry<String, String> item : searchInfo.entrySet()){
            sqlExceptSelect += buildQuery(item);
        }
        sqlExceptSelect += " order by a.created_time desc ";

        return sqlExceptSelect;
    }

    private String buildQuery(Map.Entry<String, String> item){
        if("start_time".equalsIgnoreCase(item.getKey())){
            String start_time = "'" + item.getValue().replaceAll(" ", "") + "'";
            return " and a.created_time >= " + start_time + " ";
        }
        if("end_time".equalsIgnoreCase(item.getKey())){
            String end_time = "'" + item.getValue().replaceAll(" ", "") + "'";
            return " and a.created_time <= " + end_time + " ";
        }
        if("name".equalsIgnoreCase(item.getKey())){
            return " and a." + item.getKey() + " like '%" + item.getValue() + "%'";
        }
        return " and a." + item.getKey() + " = '" + item.getValue() + "'";
    }


    static public List<Record> getCityOrgId(int orgId){
        StringBuffer sql = new StringBuffer(
                "(select " + Organization.ID + " from " + Constant.Table.ORGANIZATION);
        sql.append(" where " + Organization.ID + "=(select " + Organization.CITYCODE + " from "
                + Constant.Table.ORGANIZATION + " where " + Organization.ID + "="+orgId+"))");
        List<Record> list = Db.find(sql.toString());
        return list;
    }
}
